package com.aixs.crud.service;

import com.aixs.crud.bean.User;
import com.aixs.crud.bean.UserExample;
import com.aixs.crud.dao.UserMapper;
import com.aixs.crud.utils.MD5;
import com.aixs.crud.utils.ServerResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Service
public class IUserService {

    @Resource
    private UserMapper userMapper;

    public User login(String amount, String password) {
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andAmountEqualTo(amount);
        criteria.andPasswordEqualTo(password);
        return userMapper.selectByExample(example).get(0);
    }

    public ServerResponse register(User user) {
        User n = new User();
        n.setType(0);
        n.setPassword(MD5.md5(user.getPassword()));
        BeanUtils.copyProperties(user, n);
        int i = userMapper.insertSelective(n);
        if (i > 0) {
            return ServerResponse.createBySuccess(n);
        }
        return ServerResponse.createByErrorMsg("ע��ʧ��");
    }

    public ServerResponse getList(String url, Integer index) {
        HashMap hmap = new HashMap();
        hmap.put("url",url);
        hmap.put("index",index);
        return ServerResponse.createBySuccess(hmap);
    }
}
